package com.ddtech.business.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.constant.Del;
import com.ddtech.business.train.entity.TrainPlan;
import com.ddtech.business.train.entity.TrainPlanChapter;
import com.ddtech.business.train.mapper.TrainPlanChapterMapper;
import com.ddtech.business.train.service.TrainPlanChapterService;
import com.ddtech.business.train.service.TrainPlanResourceService;
import com.ddtech.business.train.res.TrainPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 章节表 服务实现类
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Service
class TrainPlanChapterServiceImpl extends BaseService<TrainPlanChapterMapper, TrainPlanChapter> implements TrainPlanChapterService {

    @Autowired
    private TrainPlanResourceService trainPlanResourceService;

    @Override
    public void saveChapter(TrainPlan trainPlan, TrainPlanVo trainPlanVo) {
        LambdaQueryWrapper<TrainPlanChapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainPlanChapter::getTrainPlanId, trainPlan.getId());
        List<TrainPlanChapter> courseChapterList = this.list(queryWrapper);
        List<TrainPlanChapter> courseChapters = trainPlanVo.getTrainChapter();

        List<TrainPlanChapter> collect = courseChapterList.stream().filter(item -> !courseChapters.contains(item)).collect(Collectors.toList());
        for (TrainPlanChapter trainPlanChapter : collect) {
            trainPlanChapter.baseDelete();
            trainPlanResourceService.removeResource(trainPlanChapter);
            this.saveOrUpdate(trainPlanChapter);
        }
        courseChapters.forEach(chapter -> {
            chapter.setTrainPlanId(trainPlan.getId());
            chapter.init();
            this.saveOrUpdate(chapter);
            trainPlanResourceService.saveResource(chapter);
        });
    }

    @Override
    public List<TrainPlanChapter> getChapter(String courseId) {
        LambdaQueryWrapper<TrainPlanChapter> queryWrapper = Wrappers.<TrainPlanChapter>lambdaQuery()
                .eq(TrainPlanChapter::getTrainPlanId, courseId)
                .eq(TrainPlanChapter::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(TrainPlanChapter::getSort);
        List<TrainPlanChapter> list = this.list(queryWrapper);
        list.forEach(chapter -> {
            chapter.setTrainResourceList(trainPlanResourceService.getResource(chapter.getId()));
        });
        return list;
    }

    @Override
    public List<TrainPlanChapter> getChapterList(String courseId) {
        LambdaQueryWrapper<TrainPlanChapter> queryWrapper = Wrappers.<TrainPlanChapter>lambdaQuery()
                .eq(TrainPlanChapter::getTrainPlanId, courseId)
                .eq(TrainPlanChapter::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(TrainPlanChapter::getSort);
        List<TrainPlanChapter> list = this.list(queryWrapper);
        list.forEach(chapter -> {
            chapter.setTrainResourceList(trainPlanResourceService.getResourceList(chapter.getId()));
        });
        return list;
    }
}
